{include file='public/header' /}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini full-table">
            <div class="layui-form toolbar">
                <div class="layui-form-item">

                    <div class="layui-inline" style="padding-right: 110px;">

                        <div class="layui-inline">

                            <button id="btnAddAuth" class="layui-btn icon-btn {:auth('market.seckill_time/add')}" ><i class="layui-icon">&#xe654;</i>添加时间段</button>
                        </div>
                    </div>
                </div>
            </div>

            <table id="tableUser" lay-filter="tableUser"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="options">
    <div class="layui-btn-group">

        <a class="layui-btn layui-btn-sm {:auth('market.seckill_time/edit')}" lay-event="edit" >编辑</a>
        <!--<a class="layui-btn layui-btn-sm {:auth('market.seckill_times/edit')}" lay-event="edit" >列表</a>-->
        <a class="layui-btn layui-btn-danger layui-btn-sm {:auth('market.seckill_time/delete')}" lay-event="del">删除</a>
    </div>
</script>
<!-- 表格状态列 -->

<script type="text/html" id="tableStateUser1">
    <input type="checkbox" lay-filter="ckStateUser1" value="{{d.id}}" lay-skin="switch"
           lay-text="上架|下架" {{d.status==1?'checked':''}}/>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="modelAuth">
    <form id="modelAuthForm" lay-filter="modelAuthForm" method="post" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">区间名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入权限名称" type="text" class="layui-input" maxlength="50"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <!--<div class="layui-form-item">
            <label class="layui-form-label">日期范围:</label>
            <div class="layui-input-block">
                <input type="text"  lay-verType="tips" lay-verify="required" required name="day_time" class="layui-input" id="test16" placeholder="开始 到 结束">
            </div>
        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">日期:</label>
            <div class="layui-input-block">
                <input type="text"  lay-verType="tips" lay-verify="required" required name="day_time" class="layui-input" id="test16" placeholder="开始 到 结束">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">时间范围:</label>
            <div class="layui-input-block">
                <input type="text" name="mon_time"  lay-verType="tips" lay-verify="required" required class="layui-input" id="test1" placeholder="开始时间">
            </div>
        </div>
        <div class="form-group-bottom text-center">
            <button class="layui-btn" lay-filter="modelSubmitAuth" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>

            <button type="reset" class="layui-btn layui-btn-primary">&emsp;重置&emsp;</button>

        </div>
    </form>
</script>
<!-- js部分 -->
{include file='public/footer' /}
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin','laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var laydate = layui.laydate;
        // 渲染表格
        var insTb = table.render({
            elem: '#tableUser',
            url: '{:__url("market.seckill_time/getData")}',
            page: true,
            cellMinWidth: 100,
            height:700,
            cols: [[ //表头
                {field: 'id',title: 'ID', width: 50}
                , {field: 'name', title: '标题', width: 120}
                , {field: 'day_time', title: '日期区间', width: 120}
                , {field: 'start_time', title: '开始时间'}

                , {field: 'end_time', title: '截至时间'}

                , {field: 'create_time', title: '创建时间',width:160}
                , {fixed: 'right',field: 'id', width: 140, align: 'center', title:'操作', toolbar: '#options'}
            ]]
        });

        // 搜索
        form.on('submit(formSubSearchUser)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });
        // 添加按钮点击事件
        $('#btnAddAuth').click(function () {
            showEditModel();
        });
        // 工具条点击事件
        table.on('tool(tableUser)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') {
                showEditModel(data);
            }else if (layEvent === 'del') { // 删除
                layer.confirm('确认删除吗？', function (index) {
                    layer.close(index);
                    var load = layer.load();
                    $.get("{:url('market.seckill_time/delete')}", {
                        id: data.id
                    }, function (res) {
                        layer.close(load);
                        if (res.code == 1) {
                            layer.msg(res.msg, {icon: 1}, function () {
                                obj.del();
                            })
                        } else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    });
                });
            }
        });

        // 显示表单弹窗
        function showEditModel(mAuth) {
            layer.full(admin.open({

                type: 1,
                title: (mAuth ? '修改' : '添加') + '权限',
                content: $('#modelAuth').html(),
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    var url = mAuth ? '{:__url("market.seckill_time/edit")}' : '{:__url("market.seckill_time/add")}';
                    if (mAuth && mAuth.isMenu == '1') {
                        $('#modelAuthForm input[name="isMenu"][value="1"]').prop("checked", true);
                    }
                    //日期范围
                    layui.laydate.render({
                        elem: '#test16'
                        ,min: 0
                        ,max: 7
                    });

                    //时间范围
                    layui.laydate.render({
                        elem: '#test1'
                        ,type: 'time'
                        ,range: true
                    });
                    form.val('modelAuthForm', mAuth);  // 回显数据
                    // 表单提交事件
                    form.on('submit(modelSubmitAuth)', function (data) {
                        layer.load(2);
                        $.post(url, data.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 1) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload();
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            }));
        }

        //审核
        //
        form.on('switch(ckStateUser1)', function (obj) {
            layer.load(2);
            $.post('{:__url("market.seckill/modify")}', {
                id: obj.elem.value,
                field:'status',
                value: obj.elem.checked ? 1 : 0
            }, function (res) {
                layer.closeAll('loading');
                if (res.code == 1) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });

        //置顶
        form.on('switch(ckStateUser)', function (obj) {
            layer.load(2);
            $.post('{:__url("market.seckill/modify")}', {
                id: obj.elem.value,
                is_top: obj.elem.checked ? 1 : 0
            }, function (res) {
                layer.closeAll('loading');
                if (res.code == 1) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });

    });
</script>

</body>
</html>
